/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package taghandler;

import javax.el.ELContext;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 *
 * @author Administrador
 */
public class Bucle extends SimpleTagSupport {

    private int inicio;
    private int fin;
    private String id;

    /**
     * Called by the container to invoke this tag. 
     * The implementation of this method is provided by the tag library developer,
     * and handles all tag processing, body iteration, etc.
     */
    @Override
    public void doTag() throws JspException {
        JspWriter out = getJspContext().getOut();

        ELContext ctx = getJspContext().getELContext();
        String ruta = (String) 
                ctx.getELResolver().getValue(ctx, "application", "contextPath");
        try {
            out.println(ruta);

            //out.println("    Antes   ");


            JspFragment f = getJspBody();
            if (f != null) {
                for (int i = inicio; i <= fin; i++) {
                    getJspContext().setAttribute(id, i);
                    f.invoke(out);
                }
            }

            //out.println("    Despues   ");

        } catch (java.io.IOException ex) {
            throw new JspException("Error in Bucle tag", ex);
        }
    }

    public void setInicio(int inicio) {
        this.inicio = inicio;
    }

    public void setFin(int fin) {
        this.fin = fin;
    }

    public void setId(String id) {
        this.id = id;
    }
}
